<template>
  <MainSlot v-loading="loading">
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <div class="layout">
        <TreePond class="tree" @treeselectvalue="treeselectvalue"></TreePond>
        <TableData class="tableData" :config="table_config" :tableThead="tableThead" :dataList="dataList"
          :loading="loading" @selection-change="selectionChange">
          <template v-slot:deviceName="{ row }">
            {{ row.device.deviceName }}
          </template>
          <template v-slot:enable="{ row }">
            {{ row.device.enable }}
          </template>
        </TableData>
      </div>
    </template>
    <template v-slot:pagination>
      <Pagination @size-Change="handleSizeChange" :size="size" @current-change="handleCurrentChange" :page="page"
        :total="total">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEdit :title="(initLabel || '') + '设备'" @closeDialog="closeDialog" :deviceList="deviceList"
        :addEditFlag="addEditFlag" :dialogVisible="dialogVisible" :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components/MainSlot";
import Pagination from '@/components/Pagination';
import SearchForm from '@/components/SearchForm';
import TableData from '@/components/TableData';
import BtnSlot from '@/components/BtnSlot';
import AddEdit from './components/addEdit.vue';
import MenuEquipment from '@/components/NavMenu/menu_equipment.vue';
import TreePond from '@/components/TreePond';
import distributionEquipmentForBreedingFacilities from '@/api/basicInformation/distributionEquipmentForBreedingFacilities';


export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, AddEdit, MenuEquipment, TreePond },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      selectList: [],
      addEditFlag: 'add',
      formData: {},
      searchFormData: {},
      page: 1,
      size: 10,
      total: 10,
      initLabel: '',
      btnList: ['add', 'delete'],
      menuoptionsList: [],
      dataList: [],
      tableThead: [
        { isShow: true, label: '设备名称', slotName: 'deviceName' },
        { isShow: true, label: '设备类型', slotName: 'enable' },
      ],
      dataItemList: [],
      // 配置项
      table_config: {
        isCheckBox: true,
        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '删除',
              type: 'danger',
              flag: 'delete',
              handleRow: this.handleRow
            }
          ]
        },
      },
      deviceList: [],
      formItemList: [{
        label: '设备名称',
        type: 'input',
        param: 'name',
        isShow: true
      }]
    }
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      let { data } = await distributionEquipmentForBreedingFacilities.getalldataitemselect({});
      this.deviceList = data || [];
      console.log(data)
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData
        }
      }
      distributionEquipmentForBreedingFacilities.getlist(params).then(res => {
        if (res.code == 1) {
          this.dataList = res.data.content || [];
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.queryPageByCondition();
          }
        } else {
          this.$message.warning(res.msg || '分页查询失败')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    treeselectvalue(val) {
      if (val) {
        if (!val.isGroup) {
          this.formData = JSON.parse(JSON.stringify(val));
          this.initLabel = val.facilityName || '';
        } else {
          this.formData = {};
          this.initLabel = '';
        }
        this.searchFormData.facilityNo = val.facilityNo
        val.facilityNo && this.queryPageByCondition();
      }
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },

    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    // 操作方法
    handleRow(rowData, item) {
      console.log(rowData, item);
      if (item.flag == 'delete') {
        this.delList([rowData])
      }
      this.addEditFlag = item.flag
    },// 多选方法
    selectionChange(val) {
      this.selectList = val;
    },
    delList(deleteList = [], key = 'deviceFacilityMapperId') {
      let list = []
      deleteList.forEach(item => {
        list.push(item[key])
      })
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await distributionEquipmentForBreedingFacilities.dellist(list);
        if (res.code == 1) {
          this.$message.success(res.msg || '删除成功');
          this.queryPageByCondition();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //获取选中Btn
    getBtnEvent(flag) {
      if (flag == 'add') {
        if (this.initLabel) {
          this.dialogVisible = true;
        } else {
          this.$message.warning('请选择设施')
        }
      } else if (flag == 'delete') {
        if (this.selectList.length > 0) {
          this.delList(this.selectList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  }
};
</script>
<style lang="scss" scoped>
.layout {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;

  .tableData {
    width: calc(100% - 300px);
    height: 100%;
  }

  .tree {
    width: 300px !important;
    height: 100%;
  }
}
</style>